package cn.edu.zucc.usedcarsales.control;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.usedcarsales.itf.IVehicleInformationManager;
import cn.edu.zucc.usedcarsales.model.VehicleInformation;
import cn.edu.zucc.usedcarsales.util.DBUtil;

public class VehicleInformationManager implements IVehicleInformationManager {

	@Override
	public List<VehicleInformation> getVehiclesByOwner(int userid) {
		// TODO Auto-generated method stub
		List<VehicleInformation> list = new ArrayList<VehicleInformation>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from VehicleInformation where UserID=?");
			pst.setInt(1, userid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				VehicleInformation info = new VehicleInformation(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),rs.getString(13),rs.getString(14),rs.getString(15),rs.getString(16));
				list.add(info);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return list;
		}
	}

	@Override
	public void add(VehicleInformation v) {
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = 
					con.prepareStatement("insert into VehicleInformation (SeriesID,UserID,VehicleTypeID,BelongSeries,"
							+ "GearboxType,Output,ProduceYear,OncardTime,DriveMileage,Color,GuidePrice,ExistTime,OilWear,VehicleMaterial) "
																		+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			pst.setInt(1, v.getSeriesID());
			pst.setInt(2, v.getUserID());
			pst.setInt(3, v.getVehicleTypeID());
			pst.setString(4, v.getBelongSeries());
			pst.setString(5, v.getGearBoxType());
			pst.setString(6,v.getOutput());
			pst.setString(7, v.getProduceYear());
			pst.setString(8, v.getOncardTime());
			pst.setString(9,v.getDriveMileage());
			pst.setString(10, v.getColor());
			pst.setString(11, v.getGuidePrice());
			pst.setString(12, v.getExistTime());
			pst.setString(13, v.getOilWear());
			pst.setString(14, v.getVehicleMaterial());
			pst.execute();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = 
					con.prepareStatement("delete from VehicleInformation where VehicleID=?");
			pst.setInt(1, id);
			pst.execute();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

	@Override
	public VehicleInformation getById(int id) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = 
					con.prepareStatement("select * from  VehicleInformation where VehicleID=?");
			pst.setInt(1, id);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				VehicleInformation v = new VehicleInformation(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),rs.getString(13),rs.getString(14),rs.getString(15),rs.getString(16));
				return v;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void update(VehicleInformation v) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = 
					con.prepareStatement("update VehicleInformation set UserID=?,VehicleTypeID=?,BelongSeries=?,"
							+ "GearboxType=?,Output=?,ProduceYear=?,OncardTime=?,DriveMileage=?,Color=?,GuidePrice=?,ExistTime=?,OilWear=?,VehicleMaterial=? where VehicleID=?");
			pst.setInt(1, v.getUserID());
			pst.setInt(2, v.getVehicleTypeID());
			pst.setString(3, v.getBelongSeries());
			pst.setString(4, v.getGearBoxType());
			pst.setString(5,v.getOutput());
			pst.setString(6, v.getProduceYear());
			pst.setString(7, v.getOncardTime());
			pst.setString(8,v.getDriveMileage());
			pst.setString(9, v.getColor());
			pst.setString(10, v.getGuidePrice());
			pst.setString(11, v.getExistTime());
			pst.setString(12, v.getOilWear());
			pst.setString(13, v.getVehicleMaterial());
			pst.setInt(14, v.getVehicleID());
			pst.execute();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

	@Override
	public List<VehicleInformation> getAll() {
		// TODO Auto-generated method stub
		List<VehicleInformation> list = new ArrayList<VehicleInformation>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from VehicleInformation");
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				VehicleInformation info = new VehicleInformation(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),rs.getString(13),rs.getString(14),rs.getString(15),rs.getString(16));
				list.add(info);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return list;
		}
	}

}
